<template>
  <div class="department">
    <PageForm
      :pageFormConfig="FormConfigRef"
      @resetBtnClick="resetBtnClick"
      @searchBtnClick="searchBtnClick"
    />
    <PageTable
      :pageTableConfig="pageTableConfig"
      pageName="department"
      title="部门"
      ref="pageTableRef"
      @newBtnClick="newBtnClick"
      @editBtnClick="editBtnClick"
    />
    <PageModel
      :creatOrEdit="creatOrEdit"
      :defaultInfo="defaultInfo"
      ref="pageModelRef"
      pageName="department"
      :modelConfig="modelConfigRef"
      title="部门"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import { pageTableConfig } from './pageTableConfig'
import { pageFormConfig } from './pageFormConfig'
import { pageModelConfig } from './pageModelConfig'
import PageTable from '@/components/PageTable/PageTable.vue'
import PageForm from '@/components/PageForm/PageForm.vue'
import PageModel from '@/components/PageModel/PageModel.vue'
import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModel } from '@/hooks/usePageModel'
import { useStore } from '@/store'
export default defineComponent({
  components: {
    PageTable,
    PageForm,
    PageModel
  },
  setup() {
    const store = useStore()
    const [pageTableRef, resetBtnClick, searchBtnClick] =
      usePageSearch(pageFormConfig)
    const [pageModelRef, defaultInfo, newBtnClick, editBtnClick, creatOrEdit] =
      usePageModel(undefined, undefined, pageModelConfig)
    const FormConfigRef = computed(() => {
      const parentIdItem = pageFormConfig.formItems.find(
        (item) => item.field === 'parentId'
      )
      parentIdItem!.options = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })

      return pageFormConfig
    })
    const modelConfigRef = computed(() => {
      const parentIdItem = pageModelConfig.formItems.find(
        (item) => item.field === 'parentId'
      )
      parentIdItem!.options = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })

      return pageModelConfig
    })
    return {
      pageModelRef,
      defaultInfo,
      newBtnClick,
      editBtnClick,
      creatOrEdit,
      modelConfigRef,
      FormConfigRef,
      pageTableConfig,
      pageFormConfig,
      pageTableRef,
      resetBtnClick,
      searchBtnClick
    }
  }
})
</script>

<style scoped></style>
